package com.zhupf.dynamic;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhupf
 * @date 2024年04月03日 17:06
 * @Description
 *  139. 单词拆分
 */
public class WordBreak {

    public static void main(String[] args) {
        List<String> wordDict = new ArrayList<>();
        wordDict.add("leet");
        wordDict.add("code");
        System.out.println(wordBreak("leetcode", wordDict));
    }

    public static boolean wordBreak(String s, List<String> wordDict) {
        int[] dp = new int[s.length()+1];
        dp[0] = 1;
        for (int i = 0;i<s.length() ;i++){
            for (int j = 0;j<wordDict.size() && dp[i] == 1;j++){
                if((i+wordDict.get(j).length()) > s.length()){
                    continue;
                }
                if(s.startsWith(wordDict.get(j), i)){
                    dp[i+wordDict.get(j).length()] = 1;
                }
            }
        }
        return dp[s.length()] == 1;
    }
}
